package zhongbo.java.air.flightpath.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.util.AssertUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.flightpath.mapper.FlightPathMapper;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.service.IFlightPathService;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月07日 星期一 03:21:46
 */
@Service
@Slf4j
public class FlightPathServiceImpl implements IFlightPathService<FlightPath> {

    @Autowired
    private FlightPathMapper flightPathMapper;

    @Override
    public FlightPath add(FlightPath flightPath) {
        validFlightPath(flightPath);
        AssertUtil.isNull(flightPath.getId(), "实体id不能填写");

        int insert = flightPathMapper.insert(flightPath);
        if (insert > 0) {
            return flightPath;
        }
        throw new RuntimeException("新增实体失败");
    }

    /**
     * 通用验证实体信息
     */
    private void validFlightPath(FlightPath flightPath) {
        if (flightPath == null) {
            throw new RuntimeException("实体信息不能为空");
        }
        AssertUtil.maxLength(flightPath.getIcaoCode(), 6, "ICAO码不能超过6位");
        AssertUtil.notBlank(flightPath.getIcaoCode(), "ICAO码不能为空");
    }

    @Override
    public String delete(String flightPathId) {
        if (StrUtil.isBlank(flightPathId)) {
            throw new RuntimeException("实体id不能为空");
        }
        int delete = flightPathMapper.deleteById(flightPathId);
        if (delete > 0) {
            return flightPathId;
        }
        throw new RuntimeException("删除实体失败");
    }

    @Override
    public FlightPath edit(FlightPath edit) {
        validFlightPath(edit);
        String flightPathId = edit.getId();
        if (StrUtil.isBlank(flightPathId)) {
            throw new RuntimeException("实体id不能为空");
        }
        FlightPath one = findOne(flightPathId);
        if (one == null) {
            throw new RuntimeException("实体不存在");
        }
        int update = flightPathMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改实体失败");
    }

    @Override
    public FlightPath findOne(String flightPathId) {
        if (StrUtil.isBlank(flightPathId)) {
            throw new RuntimeException("实体id不能为空");
        }
        return flightPathMapper.selectById(flightPathId);
    }

    @Override
    public PageVo<FlightPath> findPage(Integer page, Integer pageSize
            , String icao, String startTime, String endTime
            , String keyword, String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightPath> queryWrapper = new QueryWrapper<>();
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(oderByField));
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(oderByField));
            }
        }
        // 支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        // 查询icao编码
        if (StrUtil.isNotBlank(icao)) {
            queryWrapper.eq("icao_code", icao);
        }
        // 查询时间范围
        if (StrUtil.isNotBlank(startTime)) {
            queryWrapper.ge("timestamp", startTime);
        }
        if (StrUtil.isNotBlank(endTime)) {
            queryWrapper.le("timestamp", endTime);
        }
        Page<FlightPath> pageParam = new Page<>(page, pageSize);
        Page<FlightPath> flightPathPage = flightPathMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(flightPathPage);
    }

    private void validPassWord(String passWord) {
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("密码不能为空");
        }
    }

    private void validFlightPathName(String flightPathName) {
        if (StrUtil.isBlank(flightPathName)) {
            throw new RuntimeException("实体名不能为空");
        }
    }

}
